package com.ants.modules.quartz.job;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.BulkResponse;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import com.ants.common.constant.CacheConstant;
import com.ants.common.system.api.ISysBaseAPI;
import com.ants.common.system.vo.LoginUser;
import com.ants.modules.ArticleManage.dto.ArticleManageDto;
import com.ants.modules.ArticleManage.dto.SysUserVo;
import com.ants.modules.ArticleManage.service.ArticleLikeCollectionService;
import com.ants.modules.ArticleManage.service.ArticleManageService;
import com.ants.modules.articleFavorites.service.ArticleFavoritesSubService;
import com.ants.modules.articleFollow.service.ArticleFollowService;
import com.ants.modules.articleLable.entity.ArticleLable;
import com.ants.modules.articleLable.service.ArticleLableService;
import com.ants.modules.articleSort.entity.ArticleSort;
import com.ants.modules.articleSort.service.ArticleSortService;
import com.ants.modules.friendLinks.entity.FriendLinks;
import com.ants.modules.friendLinks.service.FriendLinksService;
import com.ants.modules.system.service.SysUserService;
import com.ants.modules.utils.ElasticSearchUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 检查 友联URL 是否能访问
 */
@Slf4j
public class CheckUrlJob implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    @Autowired
    FriendLinksService friendLinksService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<FriendLinks> list = friendLinksService.list();
        for (FriendLinks friendLinks : list) {
           try {
               // 创建SSLContext
               SSLContext sslContext = SSLContext.getInstance("SSL");
               sslContext.init(null, new TrustManager[]{new X509TrustManager() {
                   @Override
                   public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                   }

                   @Override
                   public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                   }

                   @Override
                   public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                       return null;
                   }
               }}, new SecureRandom());

               // 获取SSLSocketFactory
               SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

               HttpRequest httpRequest = HttpRequest.get(friendLinks.getLink()).setSSLSocketFactory(sslSocketFactory);
               HttpResponse execute = httpRequest.execute();
               int status = execute.getStatus();
               if (status != 200 && status != 301) {
                   friendLinksService.removeById(friendLinks);
               }
           }catch (Exception e) {
               friendLinksService.removeById(friendLinks);
           }
        }
    }

}
